package StudyCode.summary.textfile;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ProcessingContentsTheEntireFile {
	public static void main(String[] args) throws IOException {
		int bufferSize = 8;
		Path path = Paths.get("E:/Copy/test1.txt");
		try (SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.WRITE)) {
			ByteBuffer buffer;

			System.out.println("文件内容");
			sbc.position(0);
			buffer = ByteBuffer.allocate(bufferSize);
			String encoding = System.getProperty("file.encoding");

			buffer.rewind();
			System.out.print("[" + Charset.forName(encoding).decode(buffer) + "]");
			buffer.flip();
			System.out.println("\n读取的字节数: " + sbc.read(buffer));

		}
	}
}
